#!/bin/bash
time1=`cat /root/UPS/content.txt | head -n 1`

t1=`date -d "$time1" +%s`

t_before=`date -d '-1 hours' +%s`

t_after=`date -d '1 hours' +%s`
flag=-1

if [ $t1 -gt $t_before ]&&[ $t1 -lt $t_after ];then    
    echo $time1
    flag=1;
else
    echo "time is not correct!"
fi
if [ $flag = -1 ];then
    echo "something wrong with the UPS script";
    /root/UPS/sendemail.py;
else
    time2=`cat /root/UPS/content.txt | tail -n 1 | cut -d ' ' -f 2`;
    echo $time2;
    time_history=`cat /root/UPS/history_msg.txt | tail -n 1 | cut -d ' ' -f 2`;
    echo $time_history
    if [ $time2 = $time_history ];then
	echo "No new message, UPS script good";
    else
	/root/UPS/Shutdown.sh;
	/root/UPS/sendmail.py;
	echo "Something wrong with the Power!";
	msg=`cat /root/UPS/content.txt | tail -n 1`;
	echo $msg > history_msg.txt;
    fi

fi
